#ifndef  __BASE_DEFINE_H
#define  __BASE_DEFINE_H
#include  "para_define.h"


//---------------------------------------------------------------------------------//

#define 	_Q10(A) 						(s32)((A)*1024)												// Q10 format
#define 	_Q12(A) 						(s32)((A)*4096)												// Q12 format
#define 	_Q15(A) 						(s32)((A)*32767)											// Q15 format
#define 	_2PI							(3.1415926 * 2)	
#define 	_SQRT_3							(1.732051)
#define 	_SQRT_2							(1.414214)
#define 	_Q16_VAL						(65535)
#define 	_Q15_VAL						(32768)	
#define 	_Q12_VAL						(4096)	

//---------------------------------------------------------------------------------//

#define 	CH_IP_A							(0)	
#define 	CH_IP_B							(1)	
#define 	CH_IBUS							(2)	
#define 	CH_VBUS							(3)	
#define 	CH_VCTR							(4)	
#define 	CH_TEMP							(5)	

//---------------------------------------------------------------------------------//

#define		FRACMPY_Q15(x,y)				(((x)*(y))>>15)

#define		FRACMPY_Q10(x,y)				(((x)*(y))>>10)

#define 	DFILTER(Xn1,Xn0,Filnum)			(((Filnum*Xn1)>>15) + (((32767 - Filnum)*Xn0)>>15))

#define 	ABSFUN(Value)			 		((Value)>=0?(Value):(-(Value)))

#define 	MINFUN(varone,vartwo)    		((varone)<(vartwo)?(varone):(vartwo))

#define 	MAXFUN(varone,vartwo)    		((varone)>(vartwo)?(varone):(vartwo))

#define 	UPDNLMTFUN(Var,Max,Min)	 		{(Var)=((Var)>=(Max))?(Max):(Var);(Var)=((Var)<=(Min))?(Min):(Var);}


//---------------------------------------------------------------------------------//	

#define 	I_Value(Curr_Val)				_Q15(((Curr_Val*HW_PHASE_RES*HW_AMP_GAIN)/(HW_ADC_REF-HW_AMP_REF)))    //Unit in A

#define		Calc_Coeff_Vbus					_Q12((HW_VBUS_SDR*SW_GAIN_VBUS)/HW_ADC_REF)	//82.85
#define		Calc_Coeff_Ibus					_Q12((HW_IBUS_RES*HW_PGA_GAIN*SW_GAIN_IBUS)/HW_ADC_REF)	//45875.2
#define   	Calc_Motor_Power(A)				(s32)((A*Calc_Coeff_Vbus*Calc_Coeff_Ibus)>>15)) //116

#if (Config_Driver_Mode == Driver_HV)
#define   	Calc_Vbus_Value(A)				_Q12((A*HW_VBUS_SDR*_SQRT_2*SW_GAIN_VBUS)/HW_ADC_REF)	
#else
#define   	Calc_Vbus_Value(A)				_Q12((A*HW_VBUS_SDR*SW_GAIN_VBUS)/HW_ADC_REF)	
#endif


//---------------------------------------------------------------------------------//


#if (Config_Debug_Mode == Debug_Disable)


#if (Config_Driver_Mode == Driver_HV)

#define  	Brige_Output_Off()  			{EPWM->LOCK = EPWM_LOCK_P1B_WRITE_KEY; EPWM->MASK = 0x00003F00; IO_DRIVER_EN = 0;}
#define  	Brige_Output_On()   			{EPWM->LOCK = EPWM_LOCK_P1B_WRITE_KEY; EPWM->MASK = 0x00000000; IO_DRIVER_EN = 1;}

#else

#define  	Brige_Output_Off()  			{EPWM->LOCK = EPWM_LOCK_P1B_WRITE_KEY; EPWM->MASK = 0x00003F00;EPWM->LOCK = 0x0;}
#define  	Brige_Output_On()   			{EPWM->LOCK = EPWM_LOCK_P1B_WRITE_KEY; EPWM->MASK = 0x00000000;EPWM->LOCK = 0x0;}

#endif


#else

#define  	Brige_Output_On()   			{EPWM->LOCK = EPWM_LOCK_P1B_WRITE_KEY; EPWM->MASK = 0x00000000; EPWM->LOCK = 0x0;}
#define  	Brige_Output_Off()  			{EPWM->LOCK = EPWM_LOCK_P1B_WRITE_KEY; EPWM->CMPADT[0] = EPWM_HALFPERIOD;EPWM->CMPADT[4] = EPWM_HALFPERIOD;EPWM->CMPADT[2] = EPWM_HALFPERIOD; \
											EPWM->CON3 |= 0x00001500;}

#endif
	
//---------------------------------------------------------------------------------//

#endif


